<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form
      ref="form"
      label-width="100px"
      :model="form"
      :rules="rules"
      style="padding-right: 30px"
    >
      <el-form-item label="功能名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="英文名" prop="name_en">
        <el-input v-model="form.name_en" />
      </el-form-item>
      <el-form-item label="功能编号" prop="number_no">
        <el-input v-model="form.number_no" />
      </el-form-item>
      <el-form-item label="功能值" prop="play_val">
        <el-input v-model="form.play_val" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doPublicPlayEdit as doEdit } from '@/api/car'

  export default {
    name: 'CarPublicPlayEdit',
    data() {
      return {
        form: {
          id: '',
          name: '',
          name_en: '',
          number_no: '',
          play_val: '',
        },
        typeList: [],
        rules: {
          type_id: [
            { required: true, trigger: 'change', message: '请选择车型' },
          ],
          name: [
            { required: true, trigger: 'blur', message: '请输入功能名称' },
          ],
          number_no: [
            { required: true, trigger: 'blur', message: '请输入功能编码' },
          ],
          play_val: [
            { required: true, trigger: 'blur', message: '请输入功能值' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { message } = await doEdit(this.form)
            this.$baseMessage(message, 'success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
      updateImg(val) {
        console.log(val)
        this.form.pic = val[0]
      },
    },
  }
</script>
